package com.iss.yunpan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iss.yunpan.entity.FileCollect;
import com.iss.yunpan.entity.FileContent;
import com.iss.yunpan.entity.Users;
import com.iss.yunpan.service.FileCollectService;
import com.iss.yunpan.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CollectController {
    @Autowired
    private FileService service;
    @Autowired
    private FileCollectService fileCollectService;

    @RequestMapping("myStar")

    public String allFile() {
        return "file/fileCollectList.html";
    }

    @RequestMapping("getAllCollectFile")
    @ResponseBody//object是所有类型中的父类。只要不是form表单提交的，都需要加本行
    public Map<String, Object> getAllCollectFile(HttpSession session, String id, int page, int limit, String parentId){  //修改？
        Users user = (Users) session.getAttribute("users");
        Map<String, Object>map=new HashMap<>();
        PageHelper.startPage(page,limit);
        List<FileCollect> list=fileCollectService.getAllCollectFile(user.getUserId());                              //(id,parentId)
        PageInfo<FileCollect> pageInfo =new PageInfo<FileCollect>(list);
        System.out.println(pageInfo);
        map.put("code",0);//详见layui数据表格文档示例！
        map.put("msg","");
        map.put("count",pageInfo.getTotal());//获取总个数
        map.put("data", pageInfo.getList());//最终的数据
        return map;
    }
    @RequestMapping("collect")
    @ResponseBody//object是所有类型中的父类。只要不是form表单提交的，都需要加本行
    public Map<String, Object> collect (HttpSession session, Integer id){  //修改？
        Users user = (Users) session.getAttribute("users");
        Map<String, Object>map=new HashMap<>();
        FileContent fileById = service.getFileById(id);
        FileCollect fileCollect = new FileCollect();
        fileCollect.setCreatetime(new Date());
        fileCollect.setFileid(fileById.getId());
        fileCollect.setFileName(fileById.getFileName());
        fileCollect.setFileSize(fileById.getFileSize());
        fileCollect.setFileType(fileById.getFileType());
        fileCollect.setUploadTime(fileById.getUploadTime());
        fileCollect.setUserId(user.getUserId());

        //先检查是否收藏
        FileCollect check = fileCollectService.check(user.getUserId(), id);
        if(check!=null){
            map.put("code", 1);//使判断上传成功
            map.put("msg", "您已收藏该文件，不能重复收藏");
            return map;
        }
        fileCollectService.insert(fileCollect);

        map.put("code", 0);//使判断上传成功
        map.put("msg", "收藏成功");
        return map;
    }

    @RequestMapping("uncollect")
    @ResponseBody//object是所有类型中的父类。只要不是form表单提交的，都需要加本行
    public Map<String, Object> uncollect(Integer id){  //修改？
        Map map =new HashMap();
        fileCollectService.del(id);

        map.put("code", 0);//使判断上传成功
        map.put("msg", "取消收藏成功");
        return map;
    }
}


